package com.moore.Servlet;

import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;
import com.moore.utils.JDBCUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class GoodsForAdminServlet extends HttpServlet {
    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String responseData;
        String type = request.getParams().get("type");
        switch (type) {
            case "update" : {
                String name = request.getParams().get("name");
                String id = request.getParams().get("id");
                String key;
                try {
                    key = URLDecoder.decode(request.getParams().get("key"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                String value;
                try {
                    value = URLDecoder.decode(request.getParams().get("value"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                String sql = "update " + name + " set  `" + key + "` = ? where id = ?";
                try {
                    responseData = String.valueOf(JDBCUtils.executeUpdate(sql, value, id));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            }
            case "delete" : {
                String name = request.getParams().get("name");
                String id = request.getParams().get("id");
                String sql = "delete from " + name + " where id = ?";
                try {
                    responseData = String.valueOf(JDBCUtils.executeUpdate(sql, id));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            }
            case "addGoods" : {
                String image;
                try {
                    image = URLDecoder.decode(request.getParams().get("image"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                String name;
                try {
                    name = URLDecoder.decode(request.getParams().get("name"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                String price = request.getParams().get("price");
                String stock = request.getParams().get("inventory");
                String description = request.getParams().get("classify");
                String sql = "insert into goods (image, name, price, inventory, classify) values (?, ?, ?, ?, ?)";
                try {
                    responseData = String.valueOf(JDBCUtils.executeUpdate(sql, image, name, price, stock, description));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            }
            case "addSecKillGoods" : {
                String productId = request.getParams().get("productId");
                String startTime;
                try {
                    startTime = URLDecoder.decode(request.getParams().get("startTime"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                String endTime;
                try {
                    endTime = URLDecoder.decode(request.getParams().get("endTime"),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                String price = request.getParams().get("price");
                String limit = request.getParams().get("limit");
                String stock = request.getParams().get("stock");
                String sql = "insert into activitys (goods_id, start_time, end_time, price, `limit`, number) values (?, ?, ?, ?, ?, ?)";
                try {
                    responseData = String.valueOf(JDBCUtils.executeUpdate(sql, productId, startTime, endTime, price, limit, stock));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            }
            default:
                throw new IllegalStateException("Unexpected value: " + type);
        }
        return responseData.getBytes();
    }
}
